package com.hualife.sdk.utils;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class MyHttpClient {

    public static void myPost() throws IOException {
        //建立连接请求
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //创建post请求
        HttpPost httpPost = new HttpPost("http://eureka7002.com:6061/myServlet");
        //创建参数队列
        List<NameValuePair> nameValuePairList=new ArrayList<>();
        //准备请求参数队列
        nameValuePairList.add(new BasicNameValuePair("userName","天雁"));
        //
        UrlEncodedFormEntity urlEncodedFormEntity=new UrlEncodedFormEntity(nameValuePairList,"UTF-8");
        //讲实体封装到请求当中
        httpPost.setEntity(urlEncodedFormEntity);
        //发送请求
        CloseableHttpResponse response = httpClient.execute(httpPost);
        HttpEntity entity = response.getEntity();
        if (entity!=null){
            System.out.println("接收到的响应信息:--------"+EntityUtils.toString(entity,"UTF-8"));
        }
        //资源释放
        response.close();
        httpClient.close();

    }

    public static String myGet() throws IOException {

        //建立连接请求
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //创建get请求
        //HttpGet get=new HttpGet("https://www.baidu.com/");
        //HttpGet get=new HttpGet("https://open.work.weixin.qq.com/wwopen/sso/qrConnect?login_type=jssdk&appid=ww60f69edf84f8d24f&agentid=1000002&redirect_uri=http%3A%2F%2Fsales-int.ihxlife.com&state=STATE&lang=zh&version=1.2.4");
        HttpGet get=new HttpGet("https://open.work.weixin.qq.com/wwopen/sso/qrConnect?appid=wx7dba28868b3795c5&agentid=1000047&redirect_uri=https%3A%2F%2Fsales-int.ihxlife.com%2Fhxca%2Fexamine-int%2Findex.html%23%2Flogin&state=STATE");

        CloseableHttpResponse response = httpClient.execute(get);
        int statusCode = response.getStatusLine().getStatusCode();
        String str = "访问失败";
        //响应成功在操作
        if (statusCode==200) {
            //获取响应实体
            HttpEntity entity = response.getEntity();
            InputStream content = entity.getContent();
            str = ConvertStreamToString(content);
        }
        //释放资源
        response.close();
        httpClient.close();
        return str;
    }

    // Convert stream to string

    public static String ConvertStreamToString(InputStream is) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
        } catch (IOException e) {
            System.out.println("Error=" + e.toString());
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                System.out.println("Error=" + e.toString());
            }
        }
        return sb.toString();
    }


    public static void main(String[] args) {
        try {
            myGet();
            //myPost();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}